home *** CD-ROM | disk | FTP | other *** search
- Path: newsjunkie.ans.net!philabs!usenet
- From: abf@philabs.research.philips.com (Andrew Feldman)
- Newsgroups: comp.lang.c++
- Subject: Re: Problems extending an existing class library
- Date: Wed, 07 Feb 1996 22:07:20 GMT
- Organization: Philips Laboratories, Briarcliff, NY 10510
- Distribution: inet
- Message-ID: <4fat52$cnk@philabs.research.philips.com>
- References: <4fanik$ltq@scapa.cs.ualberta.ca>
- NNTP-Posting-Host: idrpc5.philabs.research.philips.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- diego@cs.ualberta.ca (Diego Novillo) wrote:
-
- >We are developing a compiler using a code restructuring tool that
- >provides classes for all the basic components of a program: Functions,
- >Statements, Expressions, Types, Symbols and their corresponding
- >sub-classes.
-
- >Now, we would like to create new sub-classes to specialize the existing
- >classes and the first impulse was to inherit from them because the
- >specialization was clearly an "is-a" relation.
-
- >This system parses the source code and builds a special file which
- >contains the annotated parse tree of the program. All the objects reside
- >on this binary file and the memory objects are just pointers to
- >different portions of the file. As a result, the memory objects are just
- >pointers to C data structures which hold the real implementation of the
- >system (This class library is a wrapper around the old C
- >implementation).
-
- >Since the base classes don't have constructors, we cannot build our own
- >memory objects because our constructors cannot call the base class
- >constructors to initialize the parent class.
-
- >We got around the problem by using a "has-a" relation. That is, each of
- >our classes has a pointer to the "parent" class and each time we create
- >a new object we just initialize this pointer.
-
- >We don't like this option too much, but we don't see another way around
- >the problem. Modifying the original classes is not an option either, the
- >system is large and we don't have much time to mess around with it.
-
- >Does anyone have a suggestion on how to work around this problem? Has
- >anyone faced this problem before? How did you solve it?
-
- >Thank you.
-
- >Diego.
-
-
- What I think is good in this case is using a "is-a" relation as it is.
- You CAN inherit a class from C struct; C struct doesn't have a
- constructor - so what? It must definitely have some function which
- initializes it; just call this function in the child's constructor. If
- you want to inherit many C++ classes from one C struct, maybe it would
- be useful to create a "wrapper" for this struct - derived class with
- just constructor(s) and a destructor.
-
- Hope this advise will be useful.
- Andrew.
-
-
-